<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="Filter是linphone源码的精髓，Filter可以比作过滤器，负责对数据进行不同功能的操作，如解码、编码、显示、滤噪等等. 往往一个流程需要很多个Filter来组成，只有经过一层层的过滤，才能得到【纯净】的数据. 比如视频接收显示流程，需要经过rtprecv、decoder、tee、jpegwriter、display几个Filter. 下面我们来详细地讲述Filter. 定义 struc">
<meta property="og:type" content="article">
<meta property="og:title" content="linphone源码解读之Filter">
<meta property="og:url" content="http://smallmuou.gitee.io/blog/2016/03/17/2016-03-17-linphone源码解读之Filter/index.html">
<meta property="og:site_name" content="smallmuou&#39;s blog">
<meta property="og:description" content="Filter是linphone源码的精髓，Filter可以比作过滤器，负责对数据进行不同功能的操作，如解码、编码、显示、滤噪等等. 往往一个流程需要很多个Filter来组成，只有经过一层层的过滤，才能得到【纯净】的数据. 比如视频接收显示流程，需要经过rtprecv、decoder、tee、jpegwriter、display几个Filter. 下面我们来详细地讲述Filter. 定义 struc">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-04-16T01:15:21.854Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="linphone源码解读之Filter">
<meta name="twitter:description" content="Filter是linphone源码的精髓，Filter可以比作过滤器，负责对数据进行不同功能的操作，如解码、编码、显示、滤噪等等. 往往一个流程需要很多个Filter来组成，只有经过一层层的过滤，才能得到【纯净】的数据. 比如视频接收显示流程，需要经过rtprecv、decoder、tee、jpegwriter、display几个Filter. 下面我们来详细地讲述Filter. 定义 struc">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://smallmuou.gitee.io/blog/2016/03/17/2016-03-17-linphone源码解读之Filter/"/>





  <title>linphone源码解读之Filter | smallmuou's blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">smallmuou's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"><a href="https://github.com/smallmuou">https://github.com/smallmuou</a></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blog/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2016/03/17/2016-03-17-linphone源码解读之Filter/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">linphone源码解读之Filter</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-03-17T00:00:00+08:00">
                2016-03-17
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/音视频/" itemprop="url" rel="index">
                    <span itemprop="name">音视频</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>Filter是linphone源码的精髓，Filter可以比作过滤器，负责对数据进行不同功能的操作，如解码、编码、显示、滤噪等等. 往往一个流程需要很多个Filter来组成，只有经过一层层的过滤，才能得到【纯净】的数据. 比如视频接收显示流程，需要经过rtprecv、decoder、tee、jpegwriter、display几个Filter. 下面我们来详细地讲述Filter.</p>
<h3 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h3><pre>
struct _MSFilterDesc{
    MSFilterId id;
    const char *name;
    const char *text;
    MSFilterCategory category;
    const char *enc_fmt;
    int ninputs;
    int noutputs;
    MSFilterFunc init;
    MSFilterFunc preprocess;
    MSFilterFunc process;
    MSFilterFunc postprocess;
    MSFilterFunc uninit;
    MSFilterMethod *methods;
    unsigned int flags;
};

struct _MSFilter{
    MSFilterDesc *desc; //描述，指定过滤id、名称、函数指针（初始化、预处理、处理、反初始化）
    ms_mutex_t lock;
    MSQueue **inputs; //输入源
    MSQueue **outputs; //输出源
    struct _MSFactory *factory;
    void *padding;
    void *data;
    struct _MSTicker *ticker;
    MSList *notify_callbacks;
    uint32_t last_tick;
    MSFilterStats *stats;
    int postponed_task;
    bool_t seen;
};
</pre>
PS: 每种类型的Filter仅有一个Desc，该Desc定于实现文件中，并通过EXPORT来引用.

### 创建
创建一个Filter的方式比较多，可以根据id、name、desc等等，如下是五种创建方式，足够用.
<pre>
MS2_PUBLIC MSFilter * ms_filter_create_encoder(const char *mime); //创建编码Filter
MS2_PUBLIC MSFilter * ms_filter_create_decoder(const char *mime); //创建解码Filter
MS2_PUBLIC MSFilter *ms_filter_new(MSFilterId id); //根据Id创建Filter
MS2_PUBLIC MSFilter *ms_filter_new_from_name(const char *name); //根据名称创建Filter
MS2_PUBLIC MSFilter *ms_filter_new_from_desc(MSFilterDesc *desc); //根据描述创建Filter
</pre>

<h3 id="连接"><a href="#连接" class="headerlink" title="连接"></a>连接</h3><p>在文章开头我已声明过，一个流程可能会有多个Filter，那么问题来了，我们如何关联这些Filter？这里大家可以想到化学实验课时，我们一般会使用一个导管把上一步的输出接到下一步的输入，这里也一样，我们可以通过ms_filter_link来连接2个Filter.</p>
<pre>
// 连接f1的outputs[pin1]与f2的inputs[pin2]
int ms_filter_link(MSFilter *f1, int pin1, MSFilter *f2, int pin2){
    MSQueue *q; //可以理解为导管
    ...
    q=ms_queue_new(f1,pin1,f2,pin2);
    f1->outputs[pin1]=q;
    f2->inputs[pin2]=q;
    return 0;
}
</pre>

<h3 id="断开"><a href="#断开" class="headerlink" title="断开"></a>断开</h3><p>有连接操作就有断开操作，当需要断开2个Filter又如何操作呢，那就是去掉导管</p>
<pre>
int ms_filter_unlink(MSFilter *f1, int pin1, MSFilter *f2, int pin2){
    MSQueue *q;
    ...
    q=f1->outputs[pin1];
    f1->outputs[pin1]=f2->inputs[pin2]=0;
    ms_queue_destroy(q);
    return 0;
}
</pre>

<h3 id="销毁"><a href="#销毁" class="headerlink" title="销毁"></a>销毁</h3><p>为了保证资源的正确释放，需要调用销毁接口</p>
<pre>
void ms_filter_destroy(MSFilter *f)
</pre>


<h3 id="实例-视频接收显示"><a href="#实例-视频接收显示" class="headerlink" title="实例 - 视频接收显示"></a>实例 - 视频接收显示</h3><h4 id="启动视频"><a href="#启动视频" class="headerlink" title="启动视频"></a>启动视频</h4><p>videostream.c</p>
<pre>
int video_stream_start_with_source (VideoStream *stream, RtpProfile *profile, const char *rem_rtp_ip, int rem_rtp_port,
    const char *rem_rtcp_ip, int rem_rtcp_port, int payload, int jitt_comp, MSWebCam* cam, MSFilter* source, bool_t discard_decode_error){
        ...
        /* 创建 */
        stream->ms.decoder=ms_filter_create_decoder(pt->mime_type);
        if (stream->ms.decoder==NULL){
            /* big problem: we don't have a registered decoderfor this payload...*/
            ms_error("videostream.c: No decoder available for payload %i:%s.",payload,pt->mime_type);
            return -1;
        }
        ...
        /* display logic */
        stream->output = ms_filter_new_from_name (stream->display_name);
        stream->ms.rtprecv = ms_filter_new (MS_RTP_RECV_ID);
        ms_filter_call_method(stream->ms.rtprecv,MS_RTP_RECV_SET_SESSION,stream->ms.sessions.rtp_session);

        if (stream->output_performs_decoding == FALSE) {
            stream->jpegwriter=ms_filter_new(MS_JPEG_WRITER_ID);
            if (stream->jpegwriter){
                stream->tee2=ms_filter_new(MS_TEE_ID);
            }
        }
        ...
        /* 连接 */
        ms_connection_helper_start (&ch);
        ms_connection_helper_link (&ch,stream->ms.rtprecv,-1,0);
        if (stream->output_performs_decoding == FALSE) {
            if (stream->itcsink){
                ms_connection_helper_link(&ch,stream->tee3,0,0);
                ms_filter_link(stream->tee3,1,stream->itcsink,0);
                configure_itc(stream);
            }
            ms_connection_helper_link(&ch,stream->ms.decoder,0,0);
        }
        if (stream->tee2){
            ms_connection_helper_link (&ch,stream->tee2,0,0);
            ms_filter_link(stream->tee2,1,stream->jpegwriter,0);
        }
        if (stream->output!=NULL)
            ms_connection_helper_link (&ch,stream->output,0,-1);
        ...
}
</pre>
PS: tee是一分多过滤器，一个输入多个输出
可以看出视频输出经过的过滤器依次是：ms.rtprecv -》 ms.decoder -》 tee2 -》jpegwriter-》output

ms_connection_helper_link可以理解队列，filter依次加到其尾部

#### 停止视频
videostream.c
<pre>
void
video_stream_stop (VideoStream * stream)
{
...
            } else if (stream->ms.rtprecv){
                MSConnectionHelper h;
                ms_connection_helper_start (&h);
                ms_connection_helper_unlink (&h,stream->ms.rtprecv,-1,0);
                if (stream->output_performs_decoding == FALSE) {
                    if (stream->itcsink){
                        ms_connection_helper_unlink(&h,stream->tee3,0,0);
                        ms_filter_unlink(stream->tee3,1,stream->itcsink,0);
                    }
                    ms_connection_helper_unlink (&h,stream->ms.decoder,0,0);
                }
                if (stream->tee2){
                    ms_connection_helper_unlink (&h,stream->tee2,0,0);
                    ms_filter_unlink(stream->tee2,1,stream->jpegwriter,0);
                }
                if(stream->output)
                    ms_connection_helper_unlink (&h,stream->output,0,-1);
                if (stream->tee && stream->output && stream->output2==NULL)
                    ms_filter_unlink(stream->tee,1,stream->output,1);
            }
        }
    }
...
}
</pre>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2016/03/11/2016-03-11-关于Gitlab若干权限问题/" rel="next" title="关于Gitlab若干权限问题">
                <i class="fa fa-chevron-left"></i> 关于Gitlab若干权限问题
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2016/03/22/2016-03-22-可持续化集成Travis-CI入门教程/" rel="prev" title="可持续化集成Travis-CI入门教程">
                可持续化集成Travis-CI入门教程 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">smallmuou</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">197</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/smallmuou" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-globe"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#定义"><span class="nav-number">1.</span> <span class="nav-text">定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#连接"><span class="nav-number">2.</span> <span class="nav-text">连接</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#断开"><span class="nav-number">3.</span> <span class="nav-text">断开</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#销毁"><span class="nav-number">4.</span> <span class="nav-text">销毁</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实例-视频接收显示"><span class="nav-number">5.</span> <span class="nav-text">实例 - 视频接收显示</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#启动视频"><span class="nav-number">5.1.</span> <span class="nav-text">启动视频</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">smallmuou</span>

  
</div>

    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <span id="busuanzi_container_site_pv">总访问量<span id="busuanzi_value_site_pv"></span>次</span>
    <span class="post-meta-divider">|</span>



  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
